//
//  SAUpdateInventoryCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/28.
//

#import "SAUpdateInventoryCell.h"

@interface SAUpdateInventoryCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UITextField *rightTf;

@end

@implementation SAUpdateInventoryCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self initUI];
    }
    return self;
}

-(void)refreshLeftTitle:(NSString *)leftTitle RightTitle:(NSString *)rightTitle {
    self.titleLab.text = leftTitle;
    self.rightTf.placeholder = [NSString stringWithFormat:@"请输入%@",leftTitle];
    if ([leftTitle isEqualToString:@"统一折扣率"]) {
        self.rightTf.placeholder = @"\"填80表示8折\"将所选商品折扣";
    }
    self.rightTf.text = rightTitle;
}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (string.length == 0) {
        return YES;
    }
    if ([self.titleLab.text isEqualToString:@"统一折扣率"]) {
        if([string isEqualToString:@"."]) {
            return NO;
        }
        NSString *newStr = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if ([newStr doubleValue] > 100) {
            return NO;
        }
    }
    if (textField.text.length > 10) {
        return NO;
    }

    BOOL isHaveDian =NO;
    if([textField.text containsString:@"."]) {
        isHaveDian =YES;
    }
    if(string.length > 0) {
        //当前输入的字符
        unichar single = [string characterAtIndex:0];
        // 不能输入.0-9以外的字符
        if(!((single >='0'&& single <='9') || single =='.')){
            return NO;
        }
        // 只能有一个小数点
        if(isHaveDian && single =='.') {
            return NO;
        }
        // 如果第一位是.则前面加上0.
        if((textField.text.length==0) && (single =='.')) {
            textField.text=@"0";
        }
        // 如果第一位是0则后面必须输入点，否则不能输入。
        if([textField.text hasPrefix:@"0"]) {
            if(textField.text.length>1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1,1)];
                if(![secondStr isEqualToString:@"."]) {
                    return NO;
                }
            }else{
                if(![string isEqualToString:@"."]) {
                    return NO;
                }
            }
        }
        // 小数点后最多能输入两位
        if(isHaveDian) {
            NSRange ran = [textField.text rangeOfString:@"."];
            if(range.location > ran.location) {
                if([textField.text pathExtension].length>1) {
                    return NO;
                }
            }
        }
    }
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void)textFieldEditChanged:(UITextField *)textField {
    !self.inputBlock ?: self.inputBlock(textField.text);
}


-(void)initUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.rightTf];
    [self.contentView addSubview:self.line];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(30, 0, 100, 40)];
        _titleLab.textColor = kBlackColor;
        _titleLab.text = @"库存上限";
        _titleLab.font = kBFont(15);
    }
    return _titleLab;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(0, 40, kBaseScreenWidth, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UITextField *)rightTf {
    if (!_rightTf) {
        _rightTf = [[UITextField alloc] initWithFrame:SARectMake(150, 0, 202, 40)];
        _rightTf.font = kFont(15);
        _rightTf.textColor = kGrayColor;
        _rightTf.textAlignment = NSTextAlignmentRight;
        _rightTf.keyboardType = UIKeyboardTypeNumberPad;
        _rightTf.delegate = self;
        [_rightTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _rightTf;
}

@end
